package com.qfedu.mapper;

import com.qfedu.model.Contractor;
import com.qfedu.model.Developer;
import com.qfedu.model.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/6/27 12:40
 */

public interface ProjectMapper {

    @Select("Select * from project where status = 1")
    List<Project> findAll();

    @Results(id = "projectMap", value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "pname", property = "pname"),
            @Result(column = "starttime", property = "starttime"),
            @Result(column = "endtime", property = "endtime"),
            @Result(column = "updatetime", property = "updatetime"),
            @Result(column = "did", property = "developer", javaType = Developer.class, one =
                    //FetchType.LAZY 懒加载   EAGER:立即加载
                    //select 代表的关联查询  调用指定的mapper方法
            @One(select = "com.qfedu.mapper.DeveloperMapper.findByDid", fetchType = FetchType.EAGER)
            ),
            @Result(column = "cid", property = "contractor", javaType = Contractor.class, one =
            @One(select = "com.qfedu.mapper.ContractorMapper.findConByCid", fetchType = FetchType.EAGER)
            )
    })
    @Select("Select * from project where status = 1 limit #{currentPage},#{pageSize}")
    List<Project> findPage(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize);

    @Select("select count(*) from project where status = 1")
    Long count();

    @Insert("insert into project(pname,starttime,endtime,updatetime) values(#{pname},#{starttime},#{endtime},#{updatetime})")
    int addProject(Project project);

    @ResultMap("projectMap")
    @Select("select * from project where pid = #{pid} and status = 1")
    Project findProject(int pid);

    @Update("update project set pname = #{pname},starttime = #{starttime},endtime = #{endtime},updatetime = #{updatetime}" +
            " where pid = #{pid} and status = 1")
    int edit(Project project);

    // 逻辑删除
    @Update("update project set status = 0 where pid = #{pid}")
    int deleteByPid(Integer pid);

    @Update("update project set did = #{did} where pid = #{pid}")
    void bindDid(@Param("did") String did,@Param("pid") int pid);

    @Update("update project set cid = #{cid} where pid = #{pid}")
    void bindCid(@Param("cid") String cid,@Param("pid") int pid);

    @Select("select * from project where status = 1 and did = #{did}")
    List<Project> findByDid(String did);

    @Select("select * from project where status = 1 and cid = #{cid}")
    List<Project> findByCid(String did);

    @Update("update project set did = '' where did = #{did} and status = 1")
    void deleteDid(String did);

    @Update("update project set cid = '' where cid = #{cid} and status = 1")
    void deleteCid(String cid);
}
